package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.my.contract.UpdatePwdContract;
import com.fernfx.xingtan.my.presenter.UpdatePwdPresenter;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/15.
 * 更改密码
 */

public class UpdatePwdActivity extends BaseActivity implements View.OnClickListener,UpdatePwdContract.View{
    @BindView(R.id.old_pwd_edit)
    EditText oldPwdEdit;
    @BindView(R.id.new_pwd_edit)
    EditText newPwdEdit;
    //presenter
    private UpdatePwdContract.Presenter presenter;

    public static void start(Context context) {
        Intent intent = new Intent(context,UpdatePwdActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_setting_update_pwd;
    }

    @Override
    protected void initView() {
        setToolbarTitle("修改密码");
        //init presenter
        presenter = new UpdatePwdPresenter();
        presenter.init(this);
    }

    @OnClick({R.id.submit_bt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_bt:
                String oldPwdText = trimEditText(oldPwdEdit);
                if (!RegexUtil.isPwd(oldPwdText)) {
                    ToastUtil.showCentertoast(R.string.pwd_error);
                    return;
                }
                //check new pwd
                String newPwdText = trimEditText(newPwdEdit);
                if (!RegexUtil.isPwd(newPwdText)) {
                    ToastUtil.showCentertoast(R.string.pwd_error);
                    return;
                }
                //init request
                requestArgsMap.clear();
                requestArgsMap.put("oldPassword",oldPwdText);
                requestArgsMap.put("newPassword",newPwdText);
                presenter.request(requestArgsMap);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }
}
